import React, { Component } from 'react'
import Child from './Child'

export default class Parent extends Component {

	state = {hasError:''}

	// 当Parent的子组件出现报错时候，会触发getDerivedStateFromError调用，并携带错误信息
	static getDerivedStateFromError(error){
		console.log('@@',error);//@@ TypeError: this.state.users.map is not a function
		return {hasError:error}
	}

	//这个声明周期钩子一般统计错误次数,反馈给服务器,用于通知编码进行bug解决
	componentDidCatch(error,info){
		console.log('渲染组件时出错',error);
		console.log('渲染组件时出错',info);
	}

	componentDidMount(){

	}

	componentDidUpdate(){

	}

	componentWillUnmount(){

	}


	render() {
		return (
			<div>
				<h1>Parent组件</h1>
				{this.state.hasError ? <h2>当前网络不稳定,请稍后再试</h2> : <Child/>}
			</div>
		)
	}
}
